﻿using DbControl;
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CentralMonitoring
{
    /// <summary>
    /// 设备参数配置窗体
    /// </summary>
    public partial class DeviceEdit : Form
    {    
        public int m_Id = -1;
        public int m_ParentId = 0;
        public OprType m_type = OprType.Add;
        public string m_beforName = "";
        private string m_beforCode = "";
        private devices m_devices;
        public DeviceEdit()
        {
            InitializeComponent();
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBoxName.Text) || textBoxName.Text.Length < 1)
            {
                MessageBox.Show("名称为必填项！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if(m_ParentId>0)
            {
                if (string.IsNullOrEmpty(textBoxName.Text) || textBoxName.Text.Length < 1)
                {
                    MessageBox.Show("编码为识别码与终端绑定,为必填项！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            } 
           
            if (m_type == OprType.Add)
            {
                devices dv = new devices();
                if (!string.IsNullOrEmpty(textBoxCode.Text))
                    dv.device_code = textBoxCode.Text;
                dv.device_name = textBoxName.Text;
                dv.device_position = textBoxSite.Text;
                if (!string.IsNullOrEmpty(textBoxtype.Text))
                    dv.device_type = textBoxtype.Text;
                if (!string.IsNullOrEmpty(textBoxmode.Text))
                    dv.device_mode = textBoxmode.Text;
                if (!string.IsNullOrEmpty(textBoxfactory.Text))
                    dv.device_factory = textBoxfactory.Text;
                dv.install_time = dTimePickerInstall.Value;
                dv.flag = comboBoxFlag.SelectedIndex;
                dv.create_time=DateTime.Now;
                dv.parentid = m_ParentId;
                if (OprDevice.IsExistdeviceName(textBoxName.Text))
                {
                    MessageBox.Show("名称已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;

                }
                if (OprDevice.IsExistdeviceId(textBoxCode.Text))
                {
                    MessageBox.Show("识别码已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                try
                {
                    OprDevice.Insert(dv);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("操作失败！" + ex.Message.ToString());
                } 
            }
            else
            {
                if (OprDevice.IsExistdeviceName(textBoxName.Text) && m_beforName != textBoxName.Text)
                {
                    MessageBox.Show("名称已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (!string.IsNullOrEmpty(m_beforCode))
                {
                    if (OprDevice.IsExistdeviceId(textBoxCode.Text) && m_beforCode != textBoxCode.Text)
                    {
                        MessageBox.Show("编码已存在！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                }
                try
                {                    
                    if (!string.IsNullOrEmpty(textBoxCode.Text))
                        m_devices.device_code = textBoxCode.Text;
                    m_devices.device_name = textBoxName.Text;
                    m_devices.device_position = textBoxSite.Text;
                    if (!string.IsNullOrEmpty(textBoxtype.Text))
                        m_devices.device_type = textBoxtype.Text;
                    if (!string.IsNullOrEmpty(textBoxmode.Text))
                        m_devices.device_mode = textBoxmode.Text;
                    if (!string.IsNullOrEmpty(textBoxfactory.Text))
                        m_devices.device_factory = textBoxfactory.Text;
                    m_devices.install_time = dTimePickerInstall.Value;
                    m_devices.flag = comboBoxFlag.SelectedIndex;                 
                    OprDevice.Update(m_devices);
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("操作失败！" + ex.ToString());
                }
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void DeviceEdit_Load(object sender, EventArgs e)
        {
            if (m_Id > 0)
            {
                m_devices = OprDevice.GetdevicesById(m_Id);
                if (m_devices != null)
                {
                    
                    textBoxCode.Text = m_devices.device_code;
                    textBoxName.Text = m_devices.device_name;
                    textBoxtype.Text = m_devices.device_type;
                    textBoxSite.Text = m_devices.device_position;
                    if(m_devices.install_time.HasValue)
                        dTimePickerInstall.Value = m_devices.install_time.Value;
                    textBoxmode.Text = m_devices.device_mode;
                    textBoxfactory.Text = m_devices.device_factory;
                    if(m_devices.flag.HasValue)
                    {
                        comboBoxFlag.SelectedIndex = m_devices.flag.Value;
                    }
                    if (m_devices.parentid.Value > 0)
                    {
                        m_beforCode = m_devices.device_code;
                    }
                }
            }
        }
    }
}
